<?php
declare (strict_types = 1);

namespace app\middleware;

use app\Common\model\AdminAuthRuleModel;
use think\cache\driver\Redis;
use think\facade\Request;

class adminAuth
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $action = $_SERVER['REQUEST_URI'];
        $token   = Request::header('Authorization');

        $ac = input('ac');

        // 验证码接口
        if(strpos($action,'/admin/index/verify') !== false){
            return   $next($request);
        }

        // 测试
        if(!$ac){
            // 不是登录接口
            if($action !== '/admin/index/login') {
                // token为空
                if (!$token) {
                    return json(['status' => 401, 'info' => '请重新登录1']);
                }
                $res = checkToken($token);
                if ($res['status'] !== 200) {
                    return json(['status' => 401, 'info' => $res['info']]);
                }

                // 获取控制器和操作
//                $action = '/Admin' . '/' . request()->controller() . '/' . request()->action();
//                //查找操作
//                $auth_rule = AdminAuthRuleModel::findByUsername('name', trim($action));
//
//                if (!$auth_rule) {
//                    return json(['status' => false, 'info' => '没有操作权限']);
//                }
            }
        }

        return $next($request);
    }
}
